<?php
namespace app\member\controller;
use app\ApiRest;
use app\massage\model\User;
use app\member\info\PermissionMember;
use app\member\model\Goods;
use app\member\model\Growth;
use app\member\model\Level;
use app\member\model\Member;
use app\member\model\Rights;
use app\member\model\RightsRelation;
use app\member\model\Config;
use app\member\model\Stored;
use app\member\model\StoredOrder;
use think\App;
use app\member\model\Member as model;
use think\facade\Db;


class IndexMember extends ApiRest
{

    protected $model;

    protected $config_model;

    protected $level_model;

    protected $rights_model;


    public function __construct(App $app) {

        parent::__construct($app);

        $this->model = new model();

        $this->config_model = new Config();

        $this->level_model = new Level();

        $this->rights_model = new Rights();

    }


    /**
     * @author chenniang
     * @DataTime: 2020-08-06 17:52
     * @功能说明:个人中心的会员信息
     */
    public function userMember(){

        $persisson = new PermissionMember($this->_uniacid);

        $auth      = $persisson->pAuth();

        $config = $this->config_model->configInfo(['uniacid'=>$this->_uniacid]);
        //权限
        $data['auth'] = $config['status']==1?$auth:false;
        //会员等级
        $data['level'] = $this->level_model->getUserLevel($this->getUserId(),$this->_uniacid);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-07-28 09:41
     * @功能说明:会员信息
     */
    public function memberInfo(){
        //用户会员相关信息
        $data['user_info'] = $this->model->userInfo($this->getUserId(),$this->_uniacid);
        //权益模型
        $rights_model      = new Rights();
        //当前等级的id
        $level_id          = !empty($data['user_info']['now_level']['id'])?$data['user_info']['now_level']['id']:0;
        //权益
        $data['rights']    = $rights_model->memberRights($level_id,$this->_uniacid);
        //配置
        $data['config']    = $this->config_model->configInfo(['uniacid'=>$this->_uniacid]);
        //plus会员
        $data['plus']      = $this->level_model->getPlusLvel($this->_uniacid);

        $store_model = new Stored();

        $goods_model = new Goods();

        $dis = [

            'uniacid' => $this->_uniacid,

            'status'  => 1
        ];
        //储值套餐
        $store = $store_model->storedInfo($dis);

        $data['is_store'] = !empty($store)?1:0;
        //可以购买的商品
        $can_bug = $goods_model->getCanBuyGoods($this->getUserId(),$this->_uniacid);

        $data['is_goods'] = !empty($can_bug)?1:0;

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-07-28 15:26
     * @功能说明:等级详情
     */
    public function levelInfo(){

        $dis = [

            'b.id' => $this->getUserId()

        ];
        $level_model = new Level();

        $growth_model= new Growth();
        //用户信息
        $user_info   = $this->model->memberInfoIndex($dis);
        //下一级会员等级
        $next_level  = $level_model->getMemberLevel($this->_uniacid,$user_info['growth']);
        //下一等级的成长值
        $next_growth = !empty($next_level)?$next_level['growth']:$user_info['growth'];
        //当前数据
        $data['growth'][0] = [

            'data' => $user_info['growth'],

            'name' => '当前成长值',

            'rate' => round($user_info['growth']/$next_growth*100,2)

        ];
        //下一级
        $data['growth'][1] = [

            'data' => $next_growth-$user_info['growth'],

            'name' => '升级',

            'rate' => round(($next_growth-$user_info['growth'])/$next_growth*100,2)
        ];
        //待结算金额
        $data['settled_growth']   = $growth_model->settledGrowth($this->_uniacid,$this->getUserId());
        //下级标题
        $data['next_level_title'] = !empty($next_level)?$next_level['title']:'';
        //配置
        $data['config'] = $this->config_model->configInfo(['uniacid'=>$this->_uniacid]);

        $now_level   = $level_model->getUserLevel($this->getUserId(),$this->_uniacid);

        $data['top'] = !empty($now_level)?$now_level['top']:0;

        return $this->success($data);
    }


    /**
     * @author chenniang
     * @DataTime: 2020-07-28 16:59
     * @功能说明:会员等级列表
     */
    public function levelList(){

        $dis = [

            'uniacid' => $this->_uniacid,

            'status'  => 1,

        ];

        $user_model = new User();
        //等级列表
        $data = $this->level_model->where($dis)->order('top')->select()->toArray();
        //当前等级
        $now_level = $user_model->where(['id'=>$this->getUserId()])->value('member_level');

        if(!empty($data)){

            foreach ($data as $k=>&$v){
                //下一级的成长值
                $v['next_growth'] = !empty($data[$k+1]['growth'])?$data[$k+1]['growth']:'';
                //当前等级
                $v['now_level']   = !empty($now_level)&&$v['id']==$now_level?1:0;
                //权益
                $v['rights']      = $this->rights_model->memberRights($v['id'],$v['uniacid']);

            }

        }

        $list['data']   = $data;
        //配置
        $list['config'] = $this->config_model->configInfo(['uniacid'=>$this->_uniacid]);

        return $this->success($list);
    }


    /**
     * @author chenniang
     * @DataTime: 2020-07-30 16:54
     * @功能说明:权益详情
     */
    public function rightsInfo(){

        $input = $this->_input;

        $data  = $this->rights_model->rightsInfo(['id'=>$input['id']]);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-07-30 16:57
     * @功能说明:会员配置
     */
    public function memberConfig(){

        $data = $this->config_model->configInfo(['uniacid'=>$this->_uniacid]);

        return $this->success($data);

    }

    /**
     * @author chenniang
     * @DataTime: 2020-09-10 17:52
     * @功能说明:会员二维码
     */
    public function memberQr(){

        $input = $this->_input;

       // $qr    = getCache($this->getUserId()."-member_qr",$this->_uniacid);

//        if(empty($qr)){

            $input['id'] = $this->getUserId();

            $data = longbingCreateWxCode($this->_uniacid,$input,$input['page']);

            $data = transImagesOne($data ,['qr_path'] ,$this->_uniacid);

            $qr   = $data['qr_path'];

           // setCache($this->getUserId()."-member_qr",$qr,864000,$this->_uniacid);
//        }
        return $this->success($qr);
    }











}
